﻿using DavidJalbert.TinyCarControllerAdvance;
using UnityEngine;
using UnityEngine.Serialization;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.PlayerController;
using Yoozoo.Gameplay.Liberty.Utility;

namespace Yoozoo.Gameplay.Liberty.VehicleController
{


    public class VehicleSetting : MonoBehaviour
    {
        // AI
        [Header("AI驾驶过程中车辆的最大速度 m/s")]
        public float aiMaxSpeed = 8;
        
        // Player
        [Header("玩家驾驶过程中车辆的最大速度 m/s")]
        public float playerMaxSpeed = 20;

        public InputCameraConfig inputCameraConfig;

        // [Header("相机跟随水平角")]
        // public float cameraFollowAngleH;

        public Transform frontPoint;
        public Transform backPoint;
        
        public Vector3 FrontPosition => frontPoint.position;
        public Vector3 BackPosition => backPoint.position;
        
        public Transform leftDoorOpenPoint;
        public Transform rightDoorOpenPoint;

        public Animator leftDoorAnimator;
        public Animator rightDoorAnimator;

        public Transform leftSeatPoint;
        public Transform rightSeatPoint;
        
        public Vector4 grabBound = Vector4.one;

        [FormerlySerializedAs("_carBody")] [SerializeField]
        private CarBodyPhysic carBodyPhysic;
        
        private Rigidbody _carRigidbody;
        public bool isPlayerControl;
        public PlayerInputCamera playerCamera;
        
        private void Awake()
        {
#if UNITY_EDITOR
            if (carBodyPhysic != null)
            {
                if (!LibertyTransformUtility.IsChildOf(carBodyPhysic.transform,transform))
                {
                    UnityEngine.Debug.LogError("TCCABody 不属于此车辆");      
                    return;
                }
            }
#endif
            if (carBodyPhysic == null)
            {
                carBodyPhysic = GetComponentInChildren<CarBodyPhysic>();
            }
            if (carBodyPhysic != null)
            {
                _carRigidbody = carBodyPhysic.getRigidbody();
            }
            else
            {
                _carRigidbody = GetComponentInChildren<Rigidbody>();
            }
        }

        private void Update()
        {
            if (isPlayerControl && playerCamera != null)
            {
                // 设置参数
                
            }
        }

        public void Set2InputCamera()
        {
            inputCameraConfig.followTarget.rigidbody = _carRigidbody;
            playerCamera.SetCameraFollowTarget(inputCameraConfig.followTarget);
            playerCamera.SetCameraInitializedData(inputCameraConfig.initializedData);
            playerCamera.SetCameraInputData(inputCameraConfig.cameraInputData);
        }
        
        public Vector4 GetBound()
        {
            Vector3 carPos = _carRigidbody.transform.position;
            Vector3 carForw = _carRigidbody.transform.forward;
            Vector3 carLeft = -_carRigidbody.transform.right;

            Vector3 t = carForw * grabBound.x;
            Vector3 b = -carForw * grabBound.y; 
            Vector3 l = carLeft * grabBound.z;
            Vector3 r = -carLeft * grabBound.w;

            Vector3 fl = carPos + l + t;
            Vector3 fr = carPos + r + t;
            Vector3 bl = carPos + l + b;
            Vector3 br = carPos + r + b;

            //List<Vector3> points = new List<Vector3>()
            //{
            //    fl,fr,bl,br
            //};

            Vector4 re = Vector4.zero;

            //points.Sort((a1, b1) =>
            //{
            //    return a1.x > b1.x ? 1 :-1;
            //});

            //re.x = points[0].x;
            //re.y = points[3].x;

            //points.Sort((a1, b1) =>
            //{
            //    return a1.z > b1.z ? 1 :-1;
            //});

            //re.z = points[0].z;
            //re.w = points[3].z;


            re.x = MathUtils.Min(fl.x, fr.x, bl.x, br.x);
            re.y = MathUtils.Max(fl.x, fr.x, bl.x, br.x);
            re.z = MathUtils.Min(fl.z, fr.z, bl.z, br.z);
            re.w = MathUtils.Max(fl.z, fr.z, bl.z, br.z);
            return re;
        }
        
        public Vector3 GetCameraFollowPoint()
        {
            return _carRigidbody.centerOfMass + _carRigidbody.transform.position;
        }

        public Vector3 GetCameraFollowRotation()
        {
            return _carRigidbody.transform.rotation.eulerAngles;
        }


#if UNITY_EDITOR
        private void OnDrawGizmos()
        {
            if (_carRigidbody!=null && inputCameraConfig!=null && inputCameraConfig.followTarget!=null)
            {
                Gizmos.color = Color.green;
                Gizmos.DrawSphere(GetCameraFollowPoint() + inputCameraConfig.followTarget.offsetStart,0.1f);
            }

            if (frontPoint!=null)
            {
                Gizmos.color = Color.blue;
                Gizmos.DrawSphere(frontPoint.position, 0.1f);
            }
            
            if (backPoint!=null)
            {
                Gizmos.color = Color.blue;
                Gizmos.DrawSphere(backPoint.position, 0.1f);
            }
            
            if (leftDoorOpenPoint)
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawSphere(leftDoorOpenPoint.position, 0.1f);
                Gizmos.DrawLine(leftDoorOpenPoint.position, leftDoorOpenPoint.position + leftDoorOpenPoint.forward * 1);
            }
            
            if (rightDoorOpenPoint)
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawSphere(rightDoorOpenPoint.position, 0.1f);
                Gizmos.DrawLine(rightDoorOpenPoint.position, rightDoorOpenPoint.position + rightDoorOpenPoint.forward * 1);
            }
            
            Gizmos.color = Color.red;
            if (_carRigidbody!=null)
            {
                Vector3 carPos = _carRigidbody.transform.position;
                Vector3 carForw = _carRigidbody.transform.forward;
                Vector3 carLeft = -_carRigidbody.transform.right;

                Vector3 t = carForw * grabBound.x;
                Vector3 b = -carForw * grabBound.y; 
                Vector3 l = carLeft * grabBound.z;
                Vector3 r = -carLeft * grabBound.w;

                Vector3 fl = carPos + l + t;
                Vector3 fr = carPos + r + t;
                Vector3 bl = carPos + l + b;
                Vector3 br = carPos + r + b;
                
                Gizmos.DrawSphere(fl, 0.1f);
                Gizmos.DrawSphere(fr, 0.1f);
                Gizmos.DrawSphere(bl, 0.1f);
                Gizmos.DrawSphere(br, 0.1f);
            }
            
        }
#endif

    }
}
